package com.le.tester.book.effectivejava3rdchinese.secondReading.l10;

import java.awt.*;

/**
 * createTime：2024-08-15 10:02
 * description：继承point
 */
public class ColorPoint extends Point {

    private final Color color;

    public ColorPoint(int x, int y, Color color) {
        super(x, y);
        this.color = color;
    }

    @Override
    public boolean equals(Object obj) {
        //丧失了对称性
//        if (!(obj instanceof ColorPoint)) {
//            return false;
//        }
//        return super.equals(obj) && ((ColorPoint) obj).color == color;


        if (!(obj instanceof Point)) {
            return false;
        }
        if (!(obj instanceof ColorPoint)) {
            return obj.equals(this);
        }

        return super.equals(obj) && ((ColorPoint) obj).color == color;


    }

    public static void main(String[] args) {
//        Point point = new Point(1, 2);
//        ColorPoint cp = new ColorPoint(1, 2, Color.RED);
//
//        System.out.println(point.equals(cp));
//        System.out.println(cp.equals(point));

        ColorPoint p1 = new ColorPoint(1, 2, Color.RED);
        Point p2 = new Point(1, 2);
        ColorPoint p3 = new ColorPoint(1, 2, Color.BLUE);

        System.out.println(p1.equals(p2));//true
        System.out.println(p2.equals(p3));//true
        System.out.println(p1.equals(p3));//false
    }
}
